<?php
namespace Summer\Sms\Classes;

use Overtrue\EasySms\EasySms;
use Summer\Sms\Models\RecordModel;
use Summer\Sms\Models\SceneModel;
use Summer\Sms\Models\Settings;

class SmsApi
{
    /**
     * 发送短信
     * @param $mobile
     * @param $scene_slug
     * @param null $param
     * @return bool
     * @throws \Overtrue\EasySms\Exceptions\InvalidArgumentException
     * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
     */
    public function send($mobile,$scene_slug,$param=null)
    {
        $scene=SceneModel::where('slug',$scene_slug)->first();
        $config=$this->getConfig($scene->gateway);
//        dd($config['config']);
        $easySms = new EasySms($config['config']);
        if($config['is_self']==1){
            //自定义扩展类
            $one_config=$config['one_config'];
            $nameGateway='Summer\Sms\Classes\\'.ucfirst($scene->gateway).'Gateway';
            //dd($nameGateway);
            // 注册
            $easySms->extend($scene->gateway, function($one_config)use($nameGateway){
                return new $nameGateway($one_config);
            });
        }
        $send_arr=[];
        $send_arr['content']=$send_content=$this->make_content($scene->content,$param);
        $send_arr['template']=$scene->template??'SMS_001';
        $send_arr['data']=$param;
        $aaa=$easySms->send($mobile, $send_arr);
        //存库
        $record=new RecordModel();
        $record->phone=$mobile;
        $record->scene_slug=$scene_slug;
        $record->content=$send_content;
        $record->gateway=$scene->gateway;
        if($scene->is_code==1){
            $record->code=$param['code'];
        }
        $record->save();
        return true;
    }

    /**
     * 替换短信中的参数
     * @param $content
     * @param $data
     * @return string
     */
    public function make_content($content, $data) {
        if(!is_array($data)){
            return $content;
        }
        $pairs = [];
        foreach ($data as $k => $v)
        {
            $key = "{".$k."}";
            $pairs[$key] = $v;
        }
        $result = strtr($content, $pairs); // 整个数组替换
        return $result;
    }

    /**
     * 获取配置有新增的网关需要在此添加
     * @param $scene_slug
     */
    public function getConfig($gateway_slug){
        $config = [
            // HTTP 请求的超时时间（秒）
            'timeout' => 5.0,
            // 默认发送配置
            'default' => [
                // 网关调用策略，默认：顺序调用
                'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,
                // 默认可用的发送网关
                'gateways' => [
                    $gateway_slug,
                ],
            ],
            // 可用的网关配置
            'gateways' => [
                'errorlog' => [
                    'file' => '/tmp/easy-sms.log',
                ],
            ],
        ];
        $sms_config=Settings::instance();
        //是否自定义网关
        $is_self=0;
        switch ($gateway_slug)
        {
            case 'qirui':
                $one_config['api_key']=$sms_config->qirui_api_key;
                $one_config['api_secret']=$sms_config->qirui_api_secret;
                $one_config['sign']=$sms_config->sign;
                $is_self=1;
                $config['gateways']['qirui']=$one_config;
                break;
            case 'ipyy':
                $one_config['api_key']=$sms_config->ipyy_api_key;
                $one_config['api_secret']=$sms_config->ipyy_api_secret;
                $one_config['sign']=$sms_config->sign;
                $is_self=1;
                $config['gateways']['ipyy']=$one_config;
                break;
            case 'aliyun':
                $one_config['access_key_id']=$sms_config->aliyun_access_key_id;
                $one_config['access_key_secret']=$sms_config->aliyun_access_key_secret;
                $one_config['sign_name']=$sms_config->sign;
                $config['gateways']['aliyun']=$one_config;
                break;
            default:
            break;
        }
        $return['config']=$config;
        $return['is_self']=$is_self;
        $return['one_config']=$one_config;
        return $return;
    }


}
